/**
 * Created by Administrator on 2017/4/20.
 */


var Poker = require('../Poker/Poker');
var GD = require('../Poker/GD');
var PokerPlayer = require('../Poker/player').PokerPlayer;

var Const = require('./const');
var SubCode = require('../subCode');

exports.XQGDPlayer = PokerPlayer.extend({

    _level: 2,      // 等级
    _pass: true,    // 跳过

    init: function (uid, msg) {
        this._super(uid, msg);
        this._stat._roundCards = [];
        this._pass = true;
        this._level = 2;
    },

    /***
     * 设置玩家当前的等级
     * @param level
     */
    setLevel: function (level) {
        this._level = level;
    },

    /***
     * 获取玩家当前的技能
     * @returns {number}
     */
    getLevel: function () {
        return this._level
    },

    checkSpecBomb: function (cards) {
        if (DDZ.getCardsNum(cards) != 2) {
            return false;
        }
        var costCards = DDZ.getCostCards(cards);
        var resp = false;
        ExObject.eachKeyNum(costCards, function (c, v) {
            if (c < 100) {
                if (v.point == Const.specBomb.THREE && v.num >= 2) {
                    resp = true;
                }
                if (v.point == Const.specBomb.TWO && v.num >= 2) {
                    resp = true;
                }
            }
        });
        return resp;
    },
    checkPattern: function (pattern, cards) {
        var resp = false;
        var player = this;
        if (pattern) {
            switch (pattern) {
                case Const.Pattern.SINGLE :
                    resp = DDZ.checkSingle(cards);
                    break;
                case Const.Pattern.PAIR :
                    resp = DDZ.checkPair(cards);
                    break;
                case Const.Pattern.RUN :
                    resp = DDZ.checkRun(cards);
                    break;
                case Const.Pattern.PAIRS :
                    resp = DDZ.checkPairs(cards);
                    break;
                case Const.Pattern.TRIPLE :
                    resp = DDZ.checkTriple(cards);
                    break;
                case Const.Pattern.TRIPLE_ONE :
                    resp = DDZ.checkTripleOne(cards);
                    break;
                case Const.Pattern.AIRPLANE :
                    resp = DDZ.checkAirplane(cards);
                    break;
                case Const.Pattern.FOUR_TWO :
                    resp = DDZ.checkFourTwo(cards);
                    break;
                case Const.Pattern.SPEC_BOMB :
                    resp = player.checkSpecBomb(cards);
                    break;
                case Const.Pattern.BOMB :
                    resp = DDZ.checkBomb(cards);
                    break;
                case Const.Pattern.KING_BOMB :
                    resp = DDZ.checkKingBomb(cards);
                    break;
            }
        }
        return resp;
    },

    playCards: function (cards) {
        if (ExObject.isArray(cards)) {
            cards = this.getObj(cards);
        }
        var player = this;
        ExObject.eachKeyNum(cards, function (c, n) {
            player._handCards.delCard(c);
        });
    },
    getObj: function (cards) {
        var obj = {};
        for (var i = 0; i < cards.length; i++) {
            ExObject.addNumber0(obj, cards[i], 1)
        }
        return obj;
    },
    checkMaxTriple: function (cards) {
        var num = DDZ.getCardsNum(cards);
        num /= 4;
        var costCards = DDZ.getCostCards(cards);
        var triples = {};
        ExObject.eachKeyNum(costCards, function (c, v) {
            if (v.num == 3) {
                triples[c] = 1;
            }
        });
        var resp = true;

        var maxCost = 0;
        ExObject.eachKeyNum(triples, function (c, v) {
            if (v > 0) {
                if (c > maxCost) {
                    maxCost = c;
                }
            }
        });
        for (var i = maxCost; i > maxCost - num; i--) {
            if (triples[i] != 1) {
                resp = false;
                break;
            }
        }
        if (resp) {
            return maxCost;
        } else {
            resp = true;
        }
        var minCost = maxCost;
        ExObject.eachKeyNum(triples, function (c, v) {
            if (v > 0) {
                if (c < minCost) {
                    minCost = c;
                }
            }
        });
        for (var j = minCost; j < minCost + num; j++) {
            if (triples[j] != 1) {
                resp = false;
                break;
            }
        }
        if (resp) {
            return minCost + num - 1;
        }
        return 0;
    },
    getMaxCard: function (cards, pattern) {
        var card = 0;
        if (pattern) {
            card = DDZ.getMaxCost(cards);
            switch (pattern) {
                case Const.Pattern.SINGLE :
                    break;
                case Const.Pattern.PAIR :
                    break;
                case Const.Pattern.RUN :
                    break;
                case Const.Pattern.PAIRS :
                    break;
                case Const.Pattern.TRIPLE :
                    break;
                case Const.Pattern.TRIPLE_ONE :
                    var costCards = DDZ.getCostCards(cards);
                    ExObject.eachKeyNum(costCards, function (c, v) {
                        if (v.num == 3) {
                            card = c;
                        }
                    });
                    break;
                case Const.Pattern.AIRPLANE :
                    card = this.checkMaxTriple(cards);
                    break;
                case Const.Pattern.FOUR_TWO :
                    var costCards = DDZ.getCostCards(cards);
                    ExObject.eachKeyNum(costCards, function (c, v) {
                        if (v.num == 4) {
                            card = c;
                        }
                    });
                    break;
                case Const.Pattern.SPEC_BOMB :
                    break;
                case Const.Pattern.BOMB :
                    break;
                case Const.Pattern.KING_BOMB :
                    break;
            }
        }
        return card;
    },

    /******************************************************************
     * 重载的接口
     ******************************************************************/

    getInfo: function () {
        var room = this.getRoom();
        var info = this._super();

        //info.handCards = room._settlementStatus ? {} : this._handCards.getCards();

        info.score = this._stat.score;
        //info.roundCards = this._stat._roundCards;
        info.remainScore = this._stat.score;
        info.handCardsNum = this._handCards.getCardNum();

        return info;
    },

    getReconnectInfo: function (isSelf) {
        var info = this._super(isSelf);
        if (isSelf) {
            info.handCards = this._cardObjToArray(this._handCards.getCards());
        }
        info.status = this._jiaoStatus;
        info.handCardsNum = this._handCards.getCardNum();
        info.pass = this._pass;
        info.tiStat = this._tiCard;
        info.playCards = this._lastCards;
        info.score = this._stat.score;
        info.curRate = this._curRate;
        //info.roundCards = this._stat._roundCards;
        return info;
    },

    getSettlementInfo: function () {
        var info = this._super();
        info.handCards = this._cardObjToArray(this._handCards.getCards());
        info.bomb = this._bomb;
        info.roundTi = this._roundTi;
        info.score = this._stat.score;

        return info;
    },

    settlementPattern: function () {
        var room = this.getRoom();
        var player = this;
        var subConfig = room.getSubConfig();
    },

    settlement: function () {
        this._super();

    },
    getInitHandCardNeedNum: function () {
        //var room = this.getRoom();
        return 16;
    },
    getInitHandCardPreCards: function () {
        this._super();
        var cards = [];
        /**if(this.getIndex() == 1){
            cards.push(113);
            cards.push(213);
            cards.push(313);
            cards.push(413);
            cards.push(101);
            cards.push(201);
            cards.push(301);
            cards.push(401);
        }*/
        return cards;
    },
    onStartNewRound: function () {
        this._super();
        this._curRate = 1;
        this._stat._roundCards = [];
        this._tiCard = -1;              //-1未选择，,0不踢，1踢了,
        this._tiStat = -1;              //-1可以踢，0不能踢可以跟踢，1已经踢
        this._pass = true;
        this._isDiZhu = false;
        this._bomb = 0;
        this._partner = 0;
        this._lastCards = {
            pass: -1
        };
        this._roundTi = Const.PlayerStatus.NONE;

        this._jiaoStatus = Const.PlayerStatus.NONE;
    },


    /******************************************************************
     * 重载的接口
     ******************************************************************/
    _cardObjToArray: function (arrayObj) {
        var keys = Object.keys(arrayObj);
        var temp = [];
        keys.forEach(function (v) {
            if (arrayObj[v]) {
                temp.push(v);
            }
        });
        return temp;
    },
});